home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / wb613_01.zip / WN_GFLOA.C < prev    next >
C/C++ Source or Header  |  1991-03-15  |  7KB  |  166 lines

  1. /*
  2. ** The Window BOSS's Data Clerk
  3. ** Copyright (c) 1988 - Philip A. Mongelluzzo
  4. ** All rights reserved.
  5. **
  6. ** wn_gfloat - get float from window with validation 
  7. **
  8. ** Copyright (c) 1988 - Philip A. Mongelluzzo
  9. ** All rights reserved.
  10. **
  11. */
  12.  
  13. #include "winboss.h"                    /* standard stuff */
  14.  
  15. /*
  16. *************
  17. * wn_gfloat *
  18. *************
  19. */
  20.  
  21. /*
  22. ** wn_gfloat(fun,frm,fld,wn,row,col,prmpt,atrib,fill,v,fwidth,ndec,low,high,ubuff,hlpmsg,errmsg)
  23. **
  24. **    int        fun - fucntion code (SET || XEQ)
  25. **    (WIFORM)   frm - form pointer  (actual || NFRM)
  26. **    int        fld - field # in form (actual || NFLD)
  27. **    (WINDOWPTR) wn - window pointer
  28. **    int        row - row in window where data input begins
  29. **    int        col - col in window where data input begins
  30. **    (char *) prmpt - field promt (call with NSTR for none)
  31. **    unsigned atrib - field (not prompt) atributes 
  32. **    char      fill - field fill character
  33. **    (float  *)   v - pointer to float for return value (low-high)
  34. **    int     fwidth - width of mask (maximum # of digits is 20 with sign)
  35. **    int       ndec - # of decimal places 
  36. **    float      low - minimum value (lower limit of value)
  37. **    float     high - maximum value (upper limit of value)
  38. **    (char *) ubuff - pointer to char array of fwidth+2 bytes for editing 
  39. **    (char *)hlpmsg - pointer to help message (call with NSTR for none)
  40. **    (char *)errmsg - pointer to err message (call with NSTR) for none)
  41. **
  42. ** RETURNS:
  43. **
  44. **    V via pointer.
  45. **
  46. **    NULL if error, else the non zero value returned from wn_input.
  47. **
  48. ** NOTES:
  49. **
  50. **  FUN -   fun can only be SET for form setup, or XEQ for immediate
  51. **          execution.  When called with SET, valid arguements for both
  52. **          "frm" and "fld" must be specfied.  frm is the field pointer
  53. **          returned from frmopn(), and fld is the field sequence number
  54. **          in the form for this field.  When called with XEQ frm must
  55. **          be NFRM and fld must NFLD.
  56. **
  57. **  UBUFF - Editing buffer.  Must be of sufficent size to hold the
  58. **          data as it is entered.  Typical value is the length
  59. **          of the mask + 2 bytes (strlen(mask)+2).
  60. **
  61. **          On entry, the first byte of ubuff should be 
  62. **          a null, otherwise wn_input assumes there is valid
  63. **          data there and will enter edit mode.  This can be 
  64. **          handy if there is a need for prefilled but editable
  65. **          fields.  In actual pratice, wn_input uses this
  66. **          buffer for both initial character data entry and
  67. **          subsequent editing.
  68. **
  69. **          On return, ubuff contains the actual data entered in
  70. **          character format with fill and mask characters as
  71. **          spaces (e.g. "-1240.20").
  72. **
  73. **  Calls wn_input to perform data entry.
  74. **
  75. **  Data must satisfy validation checks for function to return.
  76. **
  77. **  Calls wn_iemsg(errmsg) when vaildation fails.
  78. */
  79.  
  80. /*
  81. *************
  82. * wn_gfloat *
  83. *************
  84. */
  85.  
  86. wn_gfloat(fun,frm,fld,wn,row,col,prmpt,atrib,fill,value,fwidth,ndec,low,high,ubuff,hlpmsg,errmsg)
  87. int fun;                                /* SET or XEQ */
  88. WIFORM frm;                             /* form pointer or NFRM */
  89. int fld;                                /* field number or NFLD */
  90. WINDOWPTR wn;                           /* window to use */
  91. int row, col;                           /* position of input field */
  92. char *prmpt;                            /* prompt string */
  93. unsigned atrib;                         /* data entry atribute */
  94. char fill;                              /* fill char */
  95. float  *value;                          /* the float */
  96. int fwidth;                             /* field width */
  97. int ndec;                               /* # of decimal places */
  98. #if DLC | BORLAND | LC3
  99. double low, high;                       /* limits: low & high */
  100. #else
  101. float low, high;                        /* limits: low & high */
  102. #endif
  103. char *ubuff;                            /* returns "value" */
  104. char *hlpmsg, *errmsg;                  /* help and error messages */
  105. {
  106. double v;                               /* temp value */
  107. char mask[25];                          /* long precision */
  108. unsigned r;                             /* sscanf return value */
  109. int rv;                                 /* return value */
  110.  
  111.   if(fun != SET && fun != XEQ)          /* saftey check */
  112.     return(NULL);
  113.  
  114.   if(fun == SET) {                      /* set up */
  115.     if(frm[fld]->pself != (char *)frm[fld])
  116.       wns_ierr("wn_gfloat");            /* die if memory is mangled */
  117.     frm[fld]->wn = wn;                  /* set window */
  118.     frm[fld]->row = row;                /* set row */
  119.     frm[fld]->col = col;                /* set col */
  120.     frm[fld]->prmpt = prmpt;            /* set prompt */
  121.     frm[fld]->atrib = atrib;            /* set attribute */
  122.     frm[fld]->fill = fill;              /* set fill character */
  123.     frm[fld]->fcode = GFLOAT;           /* function code */
  124.     frm[fld]->v1.vfp = value;           /* &value */
  125.     frm[fld]->v2.vi = fwidth;           /* fwidth */
  126.     frm[fld]->v3.vi = ndec;             /* # of decimal places */
  127. #if DLC
  128.     frm[fld]->v4.vf = (float)low;       /* lower limit */
  129.     frm[fld]->v5.vf = (float)high;      /* upper limit */
  130. #else
  131.     frm[fld]->v4.vf = low;              /* lower limit */
  132.     frm[fld]->v5.vf = high;             /* upper limit */
  133. #endif
  134.     frm[fld]->v6.vcp = ubuff;           /* &ubuff */
  135.     frm[fld]->v7.vcp = hlpmsg;          /* &hlpmsg */
  136.     frm[fld]->v8.vcp = errmsg;          /* &errmsg */
  137.     return(TRUE);
  138.   }
  139.  
  140.   strcpy(mask,"FFFFFFFFFFFFFFFFFFFFF"); /* set mask */
  141.   if(fwidth > 20 || ndec > fwidth) {    /* dont allow foolishness */
  142.     *ubuff = NUL;                       /* indicate error */
  143.     return(NULL);                       /* and return */
  144.   }
  145.   mask[fwidth] = NUL;                   /* set length */
  146.   fwidth = fwidth - (ndec+1);           /* decimal position */
  147.   mask[fwidth] = '.';                   /* set decimal point */
  148. begin:
  149.   if(!(rv=wn_input(wn,row,col,prmpt,mask,fill,atrib,ubuff,hlpmsg))) {
  150.     *ubuff = NUL;                       /* indicate error */
  151.     return(NULL);                       /* indicate error */
  152.   }
  153.   if(wni_frmflg) return(TRUE);          /* wn_frmget in progress */
  154.   if(wns_escape) return(rv);            /* escape pressed ?? */
  155.   r = sscanf(ubuff, "%lf", &v);         /* convert to float */
  156.   if(r == EOF || r == 0) v = 0.0;       /* no data! */
  157.   if(v < low || v > high) {             /* bad float */
  158.     wn_iemsg(errmsg);                   /* do error message */
  159.     goto begin;                         /* and stgart over */
  160.   }
  161.   *value = v;                           /* load user value */
  162.   return(rv);                           /* all is well.. in gross sense */
  163. }
  164.  
  165. /* End */
  166.